import unittest

from ys_python.common.base.yprint import print_delimiter
from ys_python.common.mq.mq_model import MQBaseModule, MQModuleCfg, MQHandlerInitError, MQTopic, defult_MQModuleCfg


class MQTopicsTestCase(unittest.TestCase):

    @classmethod
    def setUpClass(cls) -> None:
        cls.tool = MQTopic("test")

    def test_gen_topic(self):
        self.assertEqual(self.tool.gen_topic("test", "ex", "name"), "test.name.ex")
        self.assertEqual(self.tool.gen_topic("test", "ex"), "test.test.ex")
        self.assertEqual(self.tool.gen_topic("test"), "test.test")
        self.assertEqual(self.tool.gen_topic("test", name="name"), "test.name")

    def test_gen_heart_topic(self):
        self.assertEqual(self.tool.gen_heart_topic(), "json.heart.test")

    def test_gen_cfg_ask_topic(self):
        self.assertEqual(self.tool.gen_cfg_ask_topic(), "json.cfg?test")
        self.assertEqual(self.tool.gen_cfg_ask_topic("__all"), "json.cfg?__all")


class MyTestCase(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cfg = defult_MQModuleCfg()
        cfg.name = "test"
        cls.mq = MQBaseModule(cfg)

    def __handler(self, topic, msg):
        pass

    def test_no_name(self):
        try:
            MQBaseModule(MQModuleCfg())
        except MQHandlerInitError as e:
            print(e)
            self.assertEqual(str(e), "cfg 参数内没有 name")

    def test_handler_func_operate(self):
        print_delimiter("正常增加函数")
        self.mq.add_handler_func("test", self.__handler)
        print_delimiter("重复增加函数")
        self.mq.add_handler_func("test", self.__handler)
        print_delimiter("正常删除函数")
        self.mq.del_handler_func("test")
        print_delimiter("重复删除函数")
        self.mq.del_handler_func("test")


if __name__ == '__main__':
    unittest.main()
